package com.hmjk.health.health;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.hmjk.health.HealthApp;
import com.hmjk.health.FocusFragment;
import com.hmjk.health.R;
import com.hmjk.health.background.DataManager;
import com.hmjk.health.home.BaseHomeActions;
import com.hmjk.health.views.RefreshLayout;
import com.hmjk.health.views.RefreshLayout.OnRefreshListener;
import com.hmjk.health.views.ScheduleGatherView;

public class HealthScheduleFragment extends FocusFragment implements OnClickListener{
	private final String TAG = HealthScheduleFragment.class.getSimpleName();
	private HealthApp mApp;
	private ImageView scanView;
	RefreshLayout refresh;
	private  ScheduleGatherView mScheduleGatherView; 
	
	private void logd(String info){
		Log.d(TAG,info);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		mApp = (HealthApp)getActivity().getApplication();
		
		View v = inflater.inflate(R.layout.health_schedule, container, false);
		scanView = (ImageView) v.findViewById(R.id.scan);
		scanView.setOnClickListener(this);
		
		mScheduleGatherView = (ScheduleGatherView) v.findViewById(R.id.gather_schedule);
		ImageView set = (ImageView) v.findViewById(R.id.set);
		set.setOnClickListener(this);
		
		refresh = (RefreshLayout)v.findViewById(R.id.refresh_root);
		refresh.initScroll();
		refresh.setOnRefreshListener(new HealthScheduleRefreshListener());	
		
		return v;
	}
	
	@Override
	public void setFocused(boolean set){
		logd("setFocused "+set);
		if(set == false){
			cancelRefresh();
		}
		super.setFocused(set);
	}
	
	public boolean cancelRefresh(){
		refreshing = null;
		if((refresh != null) && refresh.isRefreshing()){
			refresh.stopRefreshing();
			return true;
		}
		return false;
	}
	
	public void dataUpdate(Intent i){
		String action = i.getAction();
		if(action.equals(DataManager.DATA_UPDATE_ACTION)){
			
		}
	}
	
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.scan:{
			((BaseHomeActions)getActivity()).openQRScanerWin();
		}
		break;
		case R.id.set:{
			//TODO:open setting spcified for health schedule
			//((BaseHomeActions)getActivity()).openSettings("account","");
			if(true)
			{
				 mScheduleGatherView.addDateView("20150102");
				 mScheduleGatherView.addAddressView(1, "05:21", "jkfdhakjhajkh", "dhfkajsd经经经  ");
				 mScheduleGatherView.addAddressView(0, "04:21", "经经经 ", "dhfkajsd经经经  ");
				 mScheduleGatherView.addAddressView(5, "07:21", "呵呵昊锐   人 ", "dhfkajsd哈哈哈哈哈 ");
				 mScheduleGatherView.addDateView("20150802");
				 mScheduleGatherView.addAddressView(1, "07:21", "jkfdhakjhajkh", "dhfkajsd黑乎乎  ");
				 mScheduleGatherView.addAddressView(0, "09:21", "或繁或简 ", "dhfkajsd官方韩国  ");
				 mScheduleGatherView.addAddressView(5, "11:21", " dhfkajsd哼哼哈哈  ","经济局");
				 mScheduleGatherView.addDateView("20150902");
				 mScheduleGatherView.addAddressView(1, "22:21", "jkfdhakjhajkh", "dhfkajsd越听越 ");
				 mScheduleGatherView.addAddressView(0, "23:21", "哈哈哈哈哈哈哈 ", "dhfkaj效谢");
				 mScheduleGatherView.addAddressView(5, "25:21", "核桃肉皇太后 ", "恍恍忽忽 ");

			}
			else
			{
				
				Intent i = new Intent(getActivity(),HealthCardBloodSugarAlarmGridActivity.class);
				startActivity(i);
			}	
		}
		break;
		}
	}
	private String refreshing = null;
	class HealthScheduleRefreshListener implements OnRefreshListener{

		@Override
		public void onRefresh() {
			refreshing = null;
//			if(((CardsActivity)getActivity()).refreshList()){
//				refreshing = new String(DataManager.F_DEVICELIST);
//			}else{
//				refreshOthers();
//			}
			 mScheduleGatherView.addDateView("20160101");
			 mScheduleGatherView.addAddressView(1, "22:21", "jkfdhakjhajkh", "dhfkajsd经济  ");
			 mScheduleGatherView.addAddressView(0, "23:21", "", "dhfkajsv  ");
			 mScheduleGatherView.addAddressView(5, "25:21", "结构坚固经国家 ", "dhfkajsd很多时候的  ");
			
			 
			if(refreshing == null){
				Message m = mhandler.obtainMessage(MSG_REFRESH_CARD_AUTO_CLOSE);
				mhandler.sendMessageDelayed(m, 500);
			}
			
		}
	}
	
	private final int MSG_REFRESH_CARD_AUTO_CLOSE = 311;
	
	private Handler mhandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			switch(msg.what){
			case MSG_REFRESH_CARD_AUTO_CLOSE:{
				cancelRefresh();
			}
			break;
			}
		}
	};
}
